package com.sx.web.player.servlet;

import com.alibaba.fastjson.JSON;
import com.sx.dao.PlayerfoDAO;
import com.sx.domain.Player;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

  @WebServlet("/showPlayerServlet")
public class ShowPlayerServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doGet(request, response);
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");//设置请求的编码
    response.setContentType("text/html;charset=utf-8");//设置响应的编码
    PrintWriter out=response.getWriter();//获取响应字节打印流
    String data = "";
    String key = null;
    if(request.getParameter("username")==null){//获取待搜索的学号，其中sno为传过来的参数
      key = "1=1";//为空显示所有学生信息
    }else{

        key = "name like '%" + request.getParameter("username") + "%' ";
//        +" and team_id like '%" + request.getParameter("userid") + "%' ";
//          +" and team like '%" + request.getParameter("teamname") + "%' ";//不为空，模糊查询

    }
    try {
      System.out.println(key);
      List<Player> list = PlayerfoDAO.findByKey(key);
      System.out.println(list);
      data = JSON.toJSONString(list);
      int count = list.size();
      data="{\"code\": 0,\"msg\": \"\",\"count\": "+count+",\"data\":"+data+"}";
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    out.print(data);
  }
}
